﻿using CastingWellEquipmentGUI.Config;
using CastingWellEquipmentGUI.Dao;
using CastingWellEquipmentGUI.Model;
using CastingWellEquipmentGUI.Utils;
using CastingWellEquipmentGUI.ViewModel.Common.Command;
using CastingWellEquipmentGUI.ViewModel.Common.LogTool;
using Confluent.Kafka;
using GalaSoft.MvvmLight;
using GalaSoft.MvvmLight.Messaging;
using Newtonsoft.Json;
using System;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Media;

namespace CastingWellEquipmentGUI.ViewModel.Pages
{
    public class CreateRecipePageViewModel : ViewModelBase
    {

        //等待Kafaka改变标签值结果的消息
        private IConsumer<string, object> KafkaConsumerOfChangeValue = KafkaHelper.GetConsumerChangeValue();

        /// <summary>
        /// 验证命令
        /// </summary>
        public DelegateCommand ValidationRecipeCommand { get; set; }

        /// <summary>
        /// 保存命令
        /// </summary>
        public DelegateCommand SaveCommand { get; set; }
        /// <summary>
        /// 取消命令
        /// </summary>
        public DelegateCommand CancelCommand { get; set; }
        /// <summary>
        /// 圆棒是否选中
        /// </summary>
        private bool _RoundBarIsChecked;
        /// <summary>
        /// 圆棒是否选中
        /// </summary>
        public bool RoundBarIsChecked
        {
            get { return _RoundBarIsChecked; }
            set
            {
                _RoundBarIsChecked = value;
                RaisePropertyChanged("RoundBarIsChecked");
            }
        }
        /// <summary>
        /// 扁锭是否选中
        /// </summary>
        private bool _FlatIngotIsChecked;
        /// <summary>
        /// 扁锭是否选中
        /// </summary>
        public bool FlatIngotIsChecked
        {
            get { return _FlatIngotIsChecked; }
            set
            {
                _FlatIngotIsChecked = value;
                RaisePropertyChanged("FlatIngotIsChecked");
            }
        }
        /// <summary>
        /// 选择类别命令
        /// </summary>
        public DelegateCommand SelectTypeCommand { get; set; }
        /// <summary>
        /// 是否显示保存按钮
        /// </summary>
        private Visibility _Button_SaveVisibility = Visibility.Hidden;
        /// <summary>
        /// 是否显示保存按钮
        /// </summary>
        public Visibility Button_SaveVisibility
        {
            get { return _Button_SaveVisibility; }
            set
            {
                _Button_SaveVisibility = value;
                RaisePropertyChanged("Button_SaveVisibility");
            }
        }
        private ObservableCollection<RecipeVerificationConfig> RecipeVerificationConfigs;

        /// <summary>
        /// 被添加的配方实体类
        /// </summary>
        private RecipeTableByVerification _CreateModel = new RecipeTableByVerification();
        /// <summary>
        /// 被添加的配方实体类
        /// </summary>
        public RecipeTableByVerification CreateModel
        {
            get { return _CreateModel; }
            set
            {
                _CreateModel = value;
                Button_SaveVisibility = Visibility.Hidden;
                RaisePropertyChanged("CreateModel");
            }
        }

        public CreateRecipePageViewModel()
        {
            InitVerificationBorderBrush();
            InitCommand();
            if (SystemConfig.equipmentCategory == "圆棒")
            {
                RoundBarIsChecked = true;
                FlatIngotIsChecked = false;
            }
            else
            {
                RoundBarIsChecked = false;
                FlatIngotIsChecked = true;
            }
            CreateModel.MSC_Type = SystemConfig.equipmentCategory;
        }

        /// <summary>
        /// 初始化命令
        /// </summary>
        private void InitCommand()
        {
            InitTag();
            ValidationRecipeCommand = new DelegateCommand()
            {
                ExecuteActionObj = new Action<object>(ValidationRecipeHandler)
            };

            SaveCommand = new DelegateCommand()
            {
                ExecuteActionObj = new Action<object>(SaveHandler)
            };

            CancelCommand = new DelegateCommand()
            {
                ExecuteActionObj = new Action<object>(CancelHandler)
            };

            SelectTypeCommand = new DelegateCommand()
            {
                ExecuteActionObj = new Action<object>(SelectTypeHandler)
            };
            LoadRecipeVerificationConfigs();
        }



        /// <summary>
        /// 加载配方验证标签配置
        /// </summary>
        private void LoadRecipeVerificationConfigs()
        {
            RecipeVerificationConfigs = DapperToolsThisSystem.QueryOc<RecipeVerificationConfig>("SELECT * FROM RecipeVerificationConfig");
        }

        /// <summary>
        /// 选择类别命令
        /// </summary>
        /// <param name="obj"></param>
        private void SelectTypeHandler(object obj)
        {
            CreateModel.MSC_Type = obj.ToString();
        }

        /// <summary>
        /// 验证是否通过命令处理器
        /// </summary>
        /// <param name="obj"></param>
        private void ValidationRecipeHandler(object obj)
        {
            if (AllPass())
            {
                Button_SaveVisibility = Visibility.Visible;
                MessageBox.Show("配方验证通过，请点击保存按钮。","配方验证",MessageBoxButton.OK,MessageBoxImage.Information
                    );
            }
            else
            {
                Button_SaveVisibility = Visibility.Hidden;
            }
        }
        /// <summary>
        /// 验证是否全部通过
        /// </summary>
        /// <returns></returns>
        private bool AllPass()
        {
            InitVerificationBorderBrush();
            if (CreateModel.RecipeName == null || CreateModel.RecipeName.Trim().Length == 0)
            {
                CreateModel.RecipeNameBorderBrush = ConvertToBrush("#F70800");
                return false;
            }
            if (CreateModel.MSC_MOLDDIAMETER == 0)
            {
                CreateModel.MSC_MOLDDIAMETERBorderBrush = ConvertToBrush("#F70800");
                return false;
            }
            if (CreateModel.MSC_REV == 0)
            {
                CreateModel.MSC_REVBorderBrush = ConvertToBrush("#F70800");
                return false;
            }
            if (CreateModel.MSC_Alloy == null || CreateModel.MSC_Alloy.Trim().Length == 0)
            {
                CreateModel.MSC_AlloyBorderBrush = ConvertToBrush("#F70800");
                return false;
            }
            if (CreateModel.MSC_MoldTech == null || CreateModel.MSC_MoldTech.Trim().Length == 0)
            {
                CreateModel.MSC_MoldTechBorderBrush = ConvertToBrush("#F70800");
                return false;
            }
            if (CreateModel.MSC_NUMBERMOLDS == 0)
            {
                CreateModel.MSC_NUMBERMOLDSBorderBrush = ConvertToBrush("#F70800");
                return false;
            }
            if (CreateModel.MSC_Description == null || CreateModel.MSC_Description.Trim().Length == 0)
            {
                CreateModel.MSC_DescriptionBorderBrush = ConvertToBrush("#F70800");
                return false;
            }


            #region 开始验证PLC标签
            CreateModel.FRN_FRNTILTBACKOFFSETBorderBrush = VerificationGetColor(CreateModel.FRN_FRNTILTBACKOFFSET_DataTag,
               CreateModel.FRN_FRNTILTBACKOFFSET_Tag,
               CreateModel.FRN_FRNTILTBACKOFFSET);
            if (CreateModel.FRN_FRNTILTBACKOFFSETBorderBrush == ConvertToBrush("#F70800"))
            {
                return false;
            }
            CreateModel.FRN_FRNBATHTEMPSPBorderBrush = VerificationGetColor(CreateModel.FRN_FRNBATHTEMPSP_DataTag,
               CreateModel.FRN_FRNBATHTEMPSP_Tag,
               CreateModel.FRN_FRNBATHTEMPSP);
            if (CreateModel.FRN_FRNBATHTEMPSPBorderBrush == ConvertToBrush("#F70800"))
            {
                return false;
            }
            CreateModel.FRN_FRNBATHTEMPHILIMBorderBrush = VerificationGetColor(CreateModel.FRN_FRNBATHTEMPHILIM_DataTag,
               CreateModel.FRN_FRNBATHTEMPHILIM_Tag,
               CreateModel.FRN_FRNBATHTEMPHILIM);
            if (CreateModel.FRN_FRNBATHTEMPHILIMBorderBrush == ConvertToBrush("#F70800"))
            {
                return false;
            }
            CreateModel.FRN_FRNBATHTEMPLOLIMBorderBrush = VerificationGetColor(CreateModel.FRN_FRNBATHTEMPLOLIM_DataTag,
               CreateModel.FRN_FRNBATHTEMPLOLIM_Tag,
               CreateModel.FRN_FRNBATHTEMPLOLIM);
            if (CreateModel.FRN_FRNBATHTEMPLOLIMBorderBrush == ConvertToBrush("#F70800"))
            {
                return false;
            }
            CreateModel.TGH_TROUGHTEMPBorderBrush = VerificationGetColor(CreateModel.TGH_TROUGHTEMP_DataTag,
               CreateModel.TGH_TROUGHTEMP_Tag,
               CreateModel.TGH_TROUGHTEMP);
            if (CreateModel.TGH_TROUGHTEMPBorderBrush == ConvertToBrush("#F70800"))
            {
                return false;
            }
            CreateModel.ROD_RODSPEEDBorderBrush = VerificationGetColor(CreateModel.ROD_RODSPEED_DataTag,
               CreateModel.ROD_RODSPEED_Tag,
               CreateModel.ROD_RODSPEED);
            if (CreateModel.ROD_RODSPEEDBorderBrush == ConvertToBrush("#F70800"))
            {
                return false;
            }
            CreateModel.LEN_CASTLENGTHBorderBrush = VerificationGetColor(CreateModel.LEN_CASTLENGTH_DataTag,
               CreateModel.LEN_CASTLENGTH_Tag,
               CreateModel.LEN_CASTLENGTH);
            if (CreateModel.LEN_CASTLENGTHBorderBrush == ConvertToBrush("#F70800"))
            {
                return false;
            }
            CreateModel.MTL_DEGASTEMPBorderBrush = VerificationGetColor(CreateModel.MTL_DEGASTEMP_DataTag,
               CreateModel.MTL_DEGASTEMP_Tag,
               CreateModel.MTL_DEGASTEMP);
            if (CreateModel.MTL_DEGASTEMPBorderBrush == ConvertToBrush("#F70800"))
            {
                return false;
            }
            CreateModel.GAS_IDLECASTGASBorderBrush = VerificationGetColor(CreateModel.GAS_IDLECASTGAS_DataTag,
               CreateModel.GAS_IDLECASTGAS_Tag,
               CreateModel.GAS_IDLECASTGAS);
            if (CreateModel.GAS_IDLECASTGASBorderBrush == ConvertToBrush("#F70800"))
            {
                return false;
            }
            CreateModel.GAS_STARTCASTGASLENBorderBrush = VerificationGetColor(CreateModel.GAS_STARTCASTGASLEN_DataTag,
               CreateModel.GAS_STARTCASTGASLEN_Tag,
               CreateModel.GAS_STARTCASTGASLEN);
            if (CreateModel.GAS_STARTCASTGASLENBorderBrush == ConvertToBrush("#F70800"))
            {
                return false;
            }
            CreateModel.GAS_STARTCASTGASBorderBrush = VerificationGetColor(CreateModel.GAS_STARTCASTGAS_DataTag,
               CreateModel.GAS_STARTCASTGAS_Tag,
               CreateModel.GAS_STARTCASTGAS);
            if (CreateModel.GAS_STARTCASTGASBorderBrush == ConvertToBrush("#F70800"))
            {
                return false;
            }
            CreateModel.GAS_BEGINRAMPDOWNBorderBrush = VerificationGetColor(CreateModel.GAS_BEGINRAMPDOWN_DataTag,
               CreateModel.GAS_BEGINRAMPDOWN_Tag,
               CreateModel.GAS_BEGINRAMPDOWN);
            if (CreateModel.GAS_BEGINRAMPDOWNBorderBrush == ConvertToBrush("#F70800"))
            {
                return false;
            }
            CreateModel.GAS_ENDRAMPDOWNBorderBrush = VerificationGetColor(CreateModel.GAS_ENDRAMPDOWN_DataTag,
               CreateModel.GAS_ENDRAMPDOWN_Tag,
               CreateModel.GAS_ENDRAMPDOWN);
            if (CreateModel.GAS_ENDRAMPDOWNBorderBrush == ConvertToBrush("#F70800"))
            {
                return false;
            }
            CreateModel.GAS_RUNCASTGASBorderBrush = VerificationGetColor(CreateModel.GAS_RUNCASTGAS_DataTag,
               CreateModel.GAS_RUNCASTGAS_Tag,
               CreateModel.GAS_RUNCASTGAS);
            if (CreateModel.GAS_RUNCASTGASBorderBrush == ConvertToBrush("#F70800"))
            {
                return false;
            }
            CreateModel.MSC_COOLDOWNTIMEBorderBrush = VerificationGetColor(CreateModel.MSC_COOLDOWNTIME_DataTag,
               CreateModel.MSC_COOLDOWNTIME_Tag,
               CreateModel.MSC_COOLDOWNTIME);
            if (CreateModel.MSC_COOLDOWNTIMEBorderBrush == ConvertToBrush("#F70800"))
            {
                return false;
            }
            CreateModel.SPD_PLTNHOLDTIMEBorderBrush = VerificationGetColor(CreateModel.SPD_PLTNHOLDTIME_DataTag,
               CreateModel.SPD_PLTNHOLDTIME_Tag,
               CreateModel.SPD_PLTNHOLDTIME);
            if (CreateModel.SPD_PLTNHOLDTIMEBorderBrush == ConvertToBrush("#F70800"))
            {
                return false;
            }
            CreateModel.WTR_WTRFLOWSTARTBorderBrush = VerificationGetColor(CreateModel.WTR_WTRFLOWSTART_DataTag,
               CreateModel.WTR_WTRFLOWSTART_Tag,
               CreateModel.WTR_WTRFLOWSTART);
            if (CreateModel.WTR_WTRFLOWSTARTBorderBrush == ConvertToBrush("#F70800"))
            {
                return false;
            }
            CreateModel.WTR_WTRFLOW1LENBorderBrush = VerificationGetColor(CreateModel.WTR_WTRFLOW1LEN_DataTag,
               CreateModel.WTR_WTRFLOW1LEN_Tag,
               CreateModel.WTR_WTRFLOW1LEN);
            if (CreateModel.WTR_WTRFLOW1LENBorderBrush == ConvertToBrush("#F70800"))
            {
                return false;
            }
            CreateModel.WTR_WTRFLOW2LENBorderBrush = VerificationGetColor(CreateModel.WTR_WTRFLOW2LEN_DataTag,
              CreateModel.WTR_WTRFLOW2LEN_Tag,
              CreateModel.WTR_WTRFLOW2LEN);
            if (CreateModel.WTR_WTRFLOW2LENBorderBrush == ConvertToBrush("#F70800"))
            {
                return false;
            }
            CreateModel.WTR_WTRFLOW3LENBorderBrush = VerificationGetColor(CreateModel.WTR_WTRFLOW3LEN_DataTag,
             CreateModel.WTR_WTRFLOW3LEN_Tag,
             CreateModel.WTR_WTRFLOW3LEN);
            if (CreateModel.WTR_WTRFLOW3LENBorderBrush == ConvertToBrush("#F70800"))
            {
                return false;
            }
            CreateModel.WTR_WTRFLOW4LENBorderBrush = VerificationGetColor(CreateModel.WTR_WTRFLOW4LEN_DataTag,
             CreateModel.WTR_WTRFLOW4LEN_Tag,
             CreateModel.WTR_WTRFLOW4LEN);
            if (CreateModel.WTR_WTRFLOW4LENBorderBrush == ConvertToBrush("#F70800"))
            {
                return false;
            }
            CreateModel.WTR_WTRFLOW5LENBorderBrush = VerificationGetColor(CreateModel.WTR_WTRFLOW5LEN_DataTag,
             CreateModel.WTR_WTRFLOW5LEN_Tag,
             CreateModel.WTR_WTRFLOW5LEN);
            if (CreateModel.WTR_WTRFLOW5LENBorderBrush == ConvertToBrush("#F70800"))
            {
                return false;
            }
            CreateModel.WTR_WTRFLOW6LENBorderBrush = VerificationGetColor(CreateModel.WTR_WTRFLOW6LEN_DataTag,
             CreateModel.WTR_WTRFLOW6LEN_Tag,
             CreateModel.WTR_WTRFLOW6LEN);
            if (CreateModel.WTR_WTRFLOW6LENBorderBrush == ConvertToBrush("#F70800"))
            {
                return false;
            }
            CreateModel.WTR_WTRFLOWENDBorderBrush = VerificationGetColor(CreateModel.WTR_WTRFLOWEND_DataTag,
             CreateModel.WTR_WTRFLOWEND_Tag,
             CreateModel.WTR_WTRFLOWEND);
            if (CreateModel.WTR_WTRFLOWENDBorderBrush == ConvertToBrush("#F70800"))
            {
                return false;
            }
            CreateModel.WTR_WTRTEMPHILIMITBorderBrush = VerificationGetColor(CreateModel.WTR_WTRTEMPHILIMIT_DataTag,
             CreateModel.WTR_WTRTEMPHILIMIT_Tag,
             CreateModel.WTR_WTRTEMPHILIMIT);
            if (CreateModel.WTR_WTRTEMPHILIMITBorderBrush == ConvertToBrush("#F70800"))
            {
                return false;
            }
            CreateModel.WTR_WTRTEMPLOLIMITBorderBrush = VerificationGetColor(CreateModel.WTR_WTRTEMPLOLIMIT_DataTag,
             CreateModel.WTR_WTRTEMPLOLIMIT_Tag,
             CreateModel.WTR_WTRTEMPLOLIMIT);
            if (CreateModel.WTR_WTRTEMPLOLIMITBorderBrush == ConvertToBrush("#F70800"))
            {
                return false;
            }
            CreateModel.WTR_WTRFLOW1BorderBrush = VerificationGetColor(CreateModel.WTR_WTRFLOW1_DataTag,
             CreateModel.WTR_WTRFLOW1_Tag,
             CreateModel.WTR_WTRFLOW1);
            if (CreateModel.WTR_WTRFLOW1BorderBrush == ConvertToBrush("#F70800"))
            {
                return false;
            }
            CreateModel.WTR_WTRFLOW2BorderBrush = VerificationGetColor(CreateModel.WTR_WTRFLOW2_DataTag,
             CreateModel.WTR_WTRFLOW2_Tag,
             CreateModel.WTR_WTRFLOW2);
            if (CreateModel.WTR_WTRFLOW2BorderBrush == ConvertToBrush("#F70800"))
            {
                return false;
            }
            CreateModel.WTR_WTRFLOW3BorderBrush = VerificationGetColor(CreateModel.WTR_WTRFLOW3_DataTag,
            CreateModel.WTR_WTRFLOW3_Tag,
            CreateModel.WTR_WTRFLOW3);
            if (CreateModel.WTR_WTRFLOW3BorderBrush == ConvertToBrush("#F70800"))
            {
                return false;
            }
            CreateModel.WTR_WTRFLOW4BorderBrush = VerificationGetColor(CreateModel.WTR_WTRFLOW4_DataTag,
            CreateModel.WTR_WTRFLOW4_Tag,
            CreateModel.WTR_WTRFLOW4);
            if (CreateModel.WTR_WTRFLOW4BorderBrush == ConvertToBrush("#F70800"))
            {
                return false;
            }
            CreateModel.WTR_WTRFLOW5BorderBrush = VerificationGetColor(CreateModel.WTR_WTRFLOW5_DataTag,
            CreateModel.WTR_WTRFLOW5_Tag,
            CreateModel.WTR_WTRFLOW5);
            if (CreateModel.WTR_WTRFLOW5BorderBrush == ConvertToBrush("#F70800"))
            {
                return false;
            }
            CreateModel.WTR_WTRFLOW6BorderBrush = VerificationGetColor(CreateModel.WTR_WTRFLOW6_DataTag,
           CreateModel.WTR_WTRFLOW6_Tag,
           CreateModel.WTR_WTRFLOW6);
            if (CreateModel.WTR_WTRFLOW6BorderBrush == ConvertToBrush("#F70800"))
            {
                return false;
            }
            CreateModel.SPD_CASTSPD1LENBorderBrush = VerificationGetColor(CreateModel.SPD_CASTSPD1LEN_DataTag,
            CreateModel.SPD_CASTSPD1LEN_Tag,
            CreateModel.SPD_CASTSPD1LEN);
            if (CreateModel.SPD_CASTSPD1LENBorderBrush == ConvertToBrush("#F70800"))
            {
                return false;
            }
            CreateModel.SPD_CASTSPD2LENBorderBrush = VerificationGetColor(CreateModel.SPD_CASTSPD2LEN_DataTag,
            CreateModel.SPD_CASTSPD2LEN_Tag,
            CreateModel.SPD_CASTSPD2LEN);
            if (CreateModel.SPD_CASTSPD2LENBorderBrush == ConvertToBrush("#F70800"))
            {
                return false;
            }
            CreateModel.SPD_CASTSPD3LENBorderBrush = VerificationGetColor(CreateModel.SPD_CASTSPD3LEN_DataTag,
            CreateModel.SPD_CASTSPD3LEN_Tag,
            CreateModel.SPD_CASTSPD3LEN);
            if (CreateModel.SPD_CASTSPD3LENBorderBrush == ConvertToBrush("#F70800"))
            {
                return false;
            }
            CreateModel.SPD_CASTSPD4LENBorderBrush = VerificationGetColor(CreateModel.SPD_CASTSPD4LEN_DataTag,
            CreateModel.SPD_CASTSPD4LEN_Tag,
            CreateModel.SPD_CASTSPD4LEN);
            if (CreateModel.SPD_CASTSPD4LENBorderBrush == ConvertToBrush("#F70800"))
            {
                return false;
            }
            CreateModel.SPD_CASTSPD5LENBorderBrush = VerificationGetColor(CreateModel.SPD_CASTSPD5LEN_DataTag,
            CreateModel.SPD_CASTSPD5LEN_Tag,
            CreateModel.SPD_CASTSPD5LEN);
            if (CreateModel.SPD_CASTSPD5LENBorderBrush == ConvertToBrush("#F70800"))
            {
                return false;
            }
            CreateModel.SPD_CASTSPD6LENBorderBrush = VerificationGetColor(CreateModel.SPD_CASTSPD6LEN_DataTag,
            CreateModel.SPD_CASTSPD6LEN_Tag,
            CreateModel.SPD_CASTSPD6LEN);
            if (CreateModel.SPD_CASTSPD6LENBorderBrush == ConvertToBrush("#F70800"))
            {
                return false;
            }
            CreateModel.SPD_CASTSPDSTARTBorderBrush = VerificationGetColor(CreateModel.SPD_CASTSPDSTART_DataTag,
            CreateModel.SPD_CASTSPDSTART_Tag,
            CreateModel.SPD_CASTSPDSTART);
            if (CreateModel.SPD_CASTSPDSTARTBorderBrush == ConvertToBrush("#F70800"))
            {
                return false;
            }
            CreateModel.SPD_CASTSPD1BorderBrush = VerificationGetColor(CreateModel.SPD_CASTSPD1_DataTag,
            CreateModel.SPD_CASTSPD1_Tag,
            CreateModel.SPD_CASTSPD1);
            if (CreateModel.SPD_CASTSPD1BorderBrush == ConvertToBrush("#F70800"))
            {
                return false;
            }
            CreateModel.SPD_CASTSPD2BorderBrush = VerificationGetColor(CreateModel.SPD_CASTSPD2_DataTag,
            CreateModel.SPD_CASTSPD2_Tag,
            CreateModel.SPD_CASTSPD2);
            if (CreateModel.SPD_CASTSPD2BorderBrush == ConvertToBrush("#F70800"))
            {
                return false;
            }
            CreateModel.SPD_CASTSPD3BorderBrush = VerificationGetColor(CreateModel.SPD_CASTSPD3_DataTag,
            CreateModel.SPD_CASTSPD3_Tag,
            CreateModel.SPD_CASTSPD3);
            if (CreateModel.SPD_CASTSPD3BorderBrush == ConvertToBrush("#F70800"))
            {
                return false;
            }
            CreateModel.SPD_CASTSPD4BorderBrush = VerificationGetColor(CreateModel.SPD_CASTSPD4_DataTag,
            CreateModel.SPD_CASTSPD4_Tag,
            CreateModel.SPD_CASTSPD4);
            if (CreateModel.SPD_CASTSPD4BorderBrush == ConvertToBrush("#F70800"))
            {
                return false;
            }
            CreateModel.SPD_CASTSPD5BorderBrush = VerificationGetColor(CreateModel.SPD_CASTSPD5_DataTag,
            CreateModel.SPD_CASTSPD5_Tag,
            CreateModel.SPD_CASTSPD5);
            if (CreateModel.SPD_CASTSPD5BorderBrush == ConvertToBrush("#F70800"))
            {
                return false;
            }
            CreateModel.SPD_CASTSPD6BorderBrush = VerificationGetColor(CreateModel.SPD_CASTSPD6_DataTag,
            CreateModel.SPD_CASTSPD6_Tag,
            CreateModel.SPD_CASTSPD6);
            if (CreateModel.SPD_CASTSPD6BorderBrush == ConvertToBrush("#F70800"))
            {
                return false;
            }
            CreateModel.SPD_CASTSPDENDBorderBrush = VerificationGetColor(CreateModel.SPD_CASTSPDEND_DataTag,
            CreateModel.SPD_CASTSPDEND_Tag,
            CreateModel.SPD_CASTSPDEND);
            if (CreateModel.SPD_CASTSPDENDBorderBrush == ConvertToBrush("#F70800"))
            {
                return false;
            }
            CreateModel.LUB_PULSELUBEPULSESBorderBrush = VerificationGetColor(CreateModel.LUB_PULSELUBEPULSES_DataTag,
            CreateModel.LUB_PULSELUBEPULSES_Tag,
            CreateModel.LUB_PULSELUBEPULSES);
            if (CreateModel.LUB_PULSELUBEPULSESBorderBrush == ConvertToBrush("#F70800"))
            {
                return false;
            }
            CreateModel.LUB_PULSELUBEONTIMEBorderBrush = VerificationGetColor(CreateModel.LUB_PULSELUBEONTIME_DataTag,
            CreateModel.LUB_PULSELUBEONTIME_Tag,
            CreateModel.LUB_PULSELUBEONTIME);
            if (CreateModel.LUB_PULSELUBEONTIMEBorderBrush == ConvertToBrush("#F70800"))
            {
                return false;
            }
            CreateModel.LUB_PULSELUBEOFFTIMEBorderBrush = VerificationGetColor(CreateModel.LUB_PULSELUBEOFFTIME_DataTag,
            CreateModel.LUB_PULSELUBEOFFTIME_Tag,
            CreateModel.LUB_PULSELUBEOFFTIME);
            if (CreateModel.LUB_PULSELUBEOFFTIMEBorderBrush == ConvertToBrush("#F70800"))
            {
                return false;
            }
            CreateModel.LUB_PULSELUBECYCTIMEBorderBrush = VerificationGetColor(CreateModel.LUB_PULSELUBECYCTIME_DataTag,
            CreateModel.LUB_PULSELUBECYCTIME_Tag,
            CreateModel.LUB_PULSELUBECYCTIME);
            if (CreateModel.LUB_PULSELUBECYCTIMEBorderBrush == ConvertToBrush("#F70800"))
            {
                return false;
            }
            CreateModel.LUB_PULSELUBEPRESFIREBorderBrush = VerificationGetColor(CreateModel.LUB_PULSELUBEPRESFIRE_DataTag,
            CreateModel.LUB_PULSELUBEPRESFIRE_Tag,
            CreateModel.LUB_PULSELUBEPRESFIRE);
            if (CreateModel.LUB_PULSELUBEPRESFIREBorderBrush == ConvertToBrush("#F70800"))
            {
                return false;
            }
            CreateModel.LUB_PULSELUBEPRESRESETBorderBrush = VerificationGetColor(CreateModel.LUB_PULSELUBEPRESRESET_DataTag,
            CreateModel.LUB_PULSELUBEPRESRESET_Tag,
            CreateModel.LUB_PULSELUBEPRESRESET);
            if (CreateModel.LUB_PULSELUBEPRESRESETBorderBrush == ConvertToBrush("#F70800"))
            {
                return false;
            }
            CreateModel.LUB_PULSELUBEPRESHOLDBorderBrush = VerificationGetColor(CreateModel.LUB_PULSELUBEPRESHOLD_DataTag,
            CreateModel.LUB_PULSELUBEPRESHOLD_Tag,
            CreateModel.LUB_PULSELUBEPRESHOLD);
            if (CreateModel.LUB_PULSELUBEPRESHOLDBorderBrush == ConvertToBrush("#F70800"))
            {
                return false;
            }
            #endregion 开始验证PLC标签
            return true;
        }


    


        /// <summary>
        /// 获取验证颜色
        /// </summary>
        /// <param name="DataTag"></param>
        /// <param name="VerificationTag"></param>
        /// <param name="value"></param>
        /// <returns></returns>
        private Brush VerificationGetColor(Meta_Tag DataTag, Meta_Tag VerificationTag, float value)
        {
            SolidColorBrush solidColorBrush = ConvertToBrush("#ABADB3");
            Meta_Driver whereMeta_DriverObj = new Meta_Driver();
            whereMeta_DriverObj.TagID = DataTag.TagID;
            string sql = @"SELECT a.*,b.PlcModelName,c.BrandName,t.Address,t.DataType,t.TagID
                           FROM Meta_Tag t
                           LEFT JOIN Meta_Group d on d.GroupID = t.GroupID
                           LEFT JOIN Meta_Driver a on a.DriverID = d.DriverID
                           LEFT JOIN PlcModel b ON a.PlcModelId = b.PlcModelId
                           LEFT JOIN Brand c ON b.BrandId = c.BrandId
                           WHERE t.TagID = @TagID";
            Meta_Driver meta_Driver = DapperToolsGatewayServer.Query(sql, whereMeta_DriverObj);

            DeviceCommunication deviceCommunication = new DeviceCommunication();

            if (deviceCommunication.SetTagValue(meta_Driver.BrandName,
                                                meta_Driver.PlcModelName,
                                                meta_Driver.IP,
                                                meta_Driver.Address,
                                                meta_Driver.DataType,
                                                value.ToString()))
            {
                whereMeta_DriverObj = new Meta_Driver();
                whereMeta_DriverObj.TagID = VerificationTag.TagID;
                sql = @"SELECT a.*,b.PlcModelName,c.BrandName,t.Address,t.DataType,t.TagID
                           FROM Meta_Tag t
                           LEFT JOIN Meta_Group d on d.GroupID = t.GroupID
                           LEFT JOIN Meta_Driver a on a.DriverID = d.DriverID
                           LEFT JOIN PlcModel b ON a.PlcModelId = b.PlcModelId
                           LEFT JOIN Brand c ON b.BrandId = c.BrandId
                           WHERE t.TagID = @TagID";
                meta_Driver = DapperToolsGatewayServer.Query(sql, whereMeta_DriverObj);
                if (deviceCommunication.GetTagValue(meta_Driver.BrandName,
                                                meta_Driver.PlcModelName,
                                                meta_Driver.IP,
                                                meta_Driver.Address,
                                                meta_Driver.DataType).ToString() == "true")
                {
                    solidColorBrush = ConvertToBrush("#F70800");
                }
            }
            else {
                MessageBox.Show("与PLC连接失败！", "警告", MessageBoxButton.OK, MessageBoxImage.Warning);
            }

            //string jsonStr = JsonConvert.SerializeObject(setTagValueMsg);
            //KafkaHelper.ProduceMessage(jsonStr);
            //int WaitingTime = 0;
            //while (true)
            //{
            //    Thread.Sleep(100);

            //    //开始消费
            //    var result = KafkaConsumerOfChangeValue.Consume();

            //    if (result.Message.Value != null)
            //    {
            //        //手动提交
            //        KafkaConsumerOfChangeValue.Commit(result);
            //        //获取消息内容
            //        string resultValue = result.Message.Value.ToString();
            //        if (resultValue == "SUCCESS")
            //        {
            //            if (KafkaHelper.GetTagValueByKey(VerificationTag.TagID.ToString()) == "true")
            //            {
            //                solidColorBrush = ConvertToBrush("#F70800");
            //            }
            //        }
            //        break;
            //    }
            //    else
            //    {
            //        Thread.Sleep(900);
            //        WaitingTime++;
            //    }
            //    if (WaitingTime == 2)
            //    {
            //        MessageBox.Show("与网关服务器连接失败！", "警告", MessageBoxButton.OK, MessageBoxImage.Warning);
            //        break;
            //    }
            //}
            return solidColorBrush;
        }
        /// <summary>
        /// 保存命令处理器
        /// </summary>
        /// <param name="obj"></param>
        private void SaveHandler(object obj)
        {
            if (RecipeIsNotExist())
            {
                //SQL语句
                string sql = @"INSERT INTO [RecipeTable]
                                       ([RecipeName]
                                       ,[FRN_FRNBATHTEMPHILIM]
                                       ,[FRN_FRNBATHTEMPLOLIM]
                                       ,[FRN_FRNBATHTEMPSP]
                                       ,[FRN_FRNTILTBACKOFFSET]
                                       ,[GAS_BEGINRAMPDOWN]
                                       ,[GAS_ENDRAMPDOWN]
                                       ,[GAS_IDLECASTGAS]
                                       ,[GAS_RUNCASTGAS]
                                       ,[GAS_STARTCASTGAS]
                                       ,[GAS_STARTCASTGASLEN]
                                       ,[JET_OFFPRESLEN]
                                       ,[JET_OFFPRESLENEND]
                                       ,[JET_PRES1]
                                       ,[JET_PRES1END]
                                       ,[JET_PRES2]
                                       ,[JET_PRES2END]
                                       ,[JET_PRESLEN1]
                                       ,[JET_PRESLEN1END]
                                       ,[JET_PRESLEN2]
                                       ,[JET_PRESLEN2END]
                                       ,[JET_STARTPRES]
                                       ,[JET_STARTPRESEND]
                                       ,[LEN_CASTLENGTH]
                                       ,[LUB_LEN2]
                                       ,[LUB_LEN3]
                                       ,[LUB_LEN4]
                                       ,[LUB_PULSELUBECYCTIME]
                                       ,[LUB_PULSELUBECYCTIME2]
                                       ,[LUB_PULSELUBECYCTIME3]
                                       ,[LUB_PULSELUBECYCTIME4]
                                       ,[LUB_PULSELUBEOFFTIME]
                                       ,[LUB_PULSELUBEONTIME]
                                       ,[LUB_PULSELUBEPRESFIRE]
                                       ,[LUB_PULSELUBEPRESHOLD]
                                       ,[LUB_PULSELUBEPRESRESET]
                                       ,[LUB_PULSELUBEPULSES]
                                       ,[LUB_PULSELUBEPULSES2]
                                       ,[LUB_PULSELUBEPULSES3]
                                       ,[LUB_PULSELUBEPULSES4]
                                       ,[MLC_FILLRATE1]
                                       ,[MLC_FILLRATE1STARTLEVEL]
                                       ,[MLC_FILLRATE2]
                                       ,[MLC_FILLRATE2STARTLEVEL]
                                       ,[MLC_FILLRATE3]
                                       ,[MLC_FILLRATE3STARTLEVEL]
                                       ,[MLC_FILLRATE4]
                                       ,[MLC_FILLRATE4STARTLEVEL]
                                       ,[MLC_FILLRATESTART]
                                       ,[MLC_MOLDLVL1]
                                       ,[MLC_MOLDLVL1LEN]
                                       ,[MLC_MOLDLVL2]
                                       ,[MLC_MOLDLVL2LEN]
                                       ,[MLC_MOLDLVL3]
                                       ,[MLC_MOLDLVL3LEN]
                                       ,[MLC_MOLDLVL4]
                                       ,[MLC_MOLDLVL4LEN]
                                       ,[MLC_MOLDLVL5]
                                       ,[MLC_MOLDLVL5LEN]
                                       ,[MLC_MOLDLVL6]
                                       ,[MLC_MOLDLVL6LEN]
                                       ,[MLC_MOLDLVL7]
                                       ,[MLC_MOLDLVL7LEN]
                                       ,[MLC_MOLDLVL8]
                                       ,[MLC_MOLDLVL8LEN]
                                       ,[MLC_MOLDLVL9]
                                       ,[MLC_MOLDLVL9LEN]
                                       ,[MLC_MOLDLVLSTART]
                                       ,[MLC_PINPRESET1]
                                       ,[MLC_PINPRESET2]
                                       ,[MLC_PINPRESET3]
                                       ,[MLC_PINPRESET4]
                                       ,[MLC_PINPRESET5]
                                       ,[MLC_PINPRESET6]
                                       ,[MLC_PINPRESET7]
                                       ,[MLC_PINPRESET8]
                                       ,[MLC_PINPRESET9]
                                       ,[MLC_PINPRESET10]
                                       ,[MLC_PINPRESET11]
                                       ,[MLC_PINPRESET12]
                                       ,[MLC_PINPRESET13]
                                       ,[MLC_PINPRESET14]
                                       ,[MLC_PINPRESET15]
                                       ,[MLC_UNCNTRLDFILLDIST]
                                       ,[MTL_DEGASTEMP]
                                       ,[ROD_RODSPEED]
                                       ,[SPD_CASTSPD1]
                                       ,[SPD_CASTSPD1LEN]
                                       ,[SPD_CASTSPD2]
                                       ,[SPD_CASTSPD2LEN]
                                       ,[SPD_CASTSPD3]
                                       ,[SPD_CASTSPD3LEN]
                                       ,[SPD_CASTSPD4]
                                       ,[SPD_CASTSPD4LEN]
                                       ,[SPD_CASTSPD5]
                                       ,[SPD_CASTSPD5LEN]
                                       ,[SPD_CASTSPD6]
                                       ,[SPD_CASTSPD6LEN]
                                       ,[SPD_CASTSPD7]
                                       ,[SPD_CASTSPD7LEN]
                                       ,[SPD_CASTSPD8]
                                       ,[SPD_CASTSPD8LEN]
                                       ,[SPD_CASTSPD9]
                                       ,[SPD_CASTSPD9LEN]
                                       ,[SPD_CASTSPDEND]
                                       ,[SPD_CASTSPDSTART]
                                       ,[SPD_PLTNHOLDTIME]
                                       ,[TGH_DAMPULLSP1]
                                       ,[TGH_DAMPULLSP2]
                                       ,[TGH_TROUGHTEMP]
                                       ,[WIP_WiperOnLen]
                                       ,[WIP_WiperPosition]
                                       ,[WIP_WIPPOS1]
                                       ,[WIP_WIPPOS1LEN]
                                       ,[WIP_WIPPOS2]
                                       ,[WIP_WIPPOS2LEN]
                                       ,[WIP_WIPPOS3]
                                       ,[WIP_WIPPOS3LEN]
                                       ,[WIP_WIPPOS4]
                                       ,[WIP_WIPPOS4LEN]
                                       ,[WIP_WIPPOS5]
                                       ,[WIP_WIPPOS5LEN]
                                       ,[WIP_WIPPOS6]
                                       ,[WIP_WIPPOS6LEN]
                                       ,[WIP_WIPPOS7]
                                       ,[WIP_WIPPOS7LEN]
                                       ,[WIP_WIPPOS8]
                                       ,[WIP_WIPPOS8LEN]
                                       ,[WIP_WIPPOS9]
                                       ,[WIP_WIPPOS9LEN]
                                       ,[WIP_WIPPOSEND]
                                       ,[WIP_WIPPOSSTART]
                                       ,[WTR_WATEROFFDELAY]
                                       ,[WTR_WTRFLOW1]
                                       ,[WTR_WTRFLOW1LEN]
                                       ,[WTR_WTRFLOW2]
                                       ,[WTR_WTRFLOW2LEN]
                                       ,[WTR_WTRFLOW3]
                                       ,[WTR_WTRFLOW3LEN]
                                       ,[WTR_WTRFLOW4]
                                       ,[WTR_WTRFLOW4LEN]
                                       ,[WTR_WTRFLOW5]
                                       ,[WTR_WTRFLOW5LEN]
                                       ,[WTR_WTRFLOW6]
                                       ,[WTR_WTRFLOW6LEN]
                                       ,[WTR_WTRFLOW7]
                                       ,[WTR_WTRFLOW7LEN]
                                       ,[WTR_WTRFLOW8]
                                       ,[WTR_WTRFLOW8LEN]
                                       ,[WTR_WTRFLOW9]
                                       ,[WTR_WTRFLOW9LEN]
                                       ,[WTR_WTRFLOWEND]
                                       ,[WTR_WTRFLOWSTART]
                                       ,[WTR_WTRTEMPHILIMIT]
                                       ,[WTR_WTRTEMPLOLIMIT]
                                       ,[MSC_COOLDOWNTIME]
                                       ,[MSC_MOLDDIAMETER]
                                       ,[MSC_MOLDHEIGHT]
                                       ,[MSC_MOLDTHICKNESS]
                                       ,[MSC_MOLDWIDTH]
                                       ,[MSC_RECNAME]
                                       ,[MSC_NUMBERMOLDS]
                                       ,[MSC_REV]
                                       ,[MSC_UserDef1]
                                       ,[MSC_UserDef2]
                                       ,[MSC_UserDef3]
                                       ,[MSC_UserDef4]
                                       ,[MSC_UserDef5]
                                       ,[MSC_UserDef6]
                                       ,[MSC_UserDef7]
                                       ,[MSC_UserDef8]
                                       ,[MSC_UserDef9]
                                       ,[MSC_UserDef10]
                                       ,[MSC_Description]
                                       ,[MSC_Alloy]
                                       ,[MSC_MoldTech]
                                       ,[MSC_DateCreated]
                                       ,[MSC_DateLastUsed]
                                       ,[MSC_CreatedBy]
                                       ,[MSC_Type])
                                 VALUES
                                       (@RecipeName
                                       ,@FRN_FRNBATHTEMPHILIM
                                       ,@FRN_FRNBATHTEMPLOLIM
                                       ,@FRN_FRNBATHTEMPSP
                                       ,@FRN_FRNTILTBACKOFFSET
                                       ,@GAS_BEGINRAMPDOWN
                                       ,@GAS_ENDRAMPDOWN
                                       ,@GAS_IDLECASTGAS
                                       ,@GAS_RUNCASTGAS
                                       ,@GAS_STARTCASTGAS
                                       ,@GAS_STARTCASTGASLEN
                                       ,@JET_OFFPRESLEN
                                       ,@JET_OFFPRESLENEND
                                       ,@JET_PRES1
                                       ,@JET_PRES1END
                                       ,@JET_PRES2
                                       ,@JET_PRES2END
                                       ,@JET_PRESLEN1
                                       ,@JET_PRESLEN1END
                                       ,@JET_PRESLEN2
                                       ,@JET_PRESLEN2END
                                       ,@JET_STARTPRES
                                       ,@JET_STARTPRESEND
                                       ,@LEN_CASTLENGTH
                                       ,@LUB_LEN2
                                       ,@LUB_LEN3
                                       ,@LUB_LEN4
                                       ,@LUB_PULSELUBECYCTIME
                                       ,@LUB_PULSELUBECYCTIME2
                                       ,@LUB_PULSELUBECYCTIME3
                                       ,@LUB_PULSELUBECYCTIME4
                                       ,@LUB_PULSELUBEOFFTIME
                                       ,@LUB_PULSELUBEONTIME
                                       ,@LUB_PULSELUBEPRESFIRE
                                       ,@LUB_PULSELUBEPRESHOLD
                                       ,@LUB_PULSELUBEPRESRESET
                                       ,@LUB_PULSELUBEPULSES
                                       ,@LUB_PULSELUBEPULSES2
                                       ,@LUB_PULSELUBEPULSES3
                                       ,@LUB_PULSELUBEPULSES4
                                       ,@MLC_FILLRATE1
                                       ,@MLC_FILLRATE1STARTLEVEL
                                       ,@MLC_FILLRATE2
                                       ,@MLC_FILLRATE2STARTLEVEL
                                       ,@MLC_FILLRATE3
                                       ,@MLC_FILLRATE3STARTLEVEL
                                       ,@MLC_FILLRATE4
                                       ,@MLC_FILLRATE4STARTLEVEL
                                       ,@MLC_FILLRATESTART
                                       ,@MLC_MOLDLVL1
                                       ,@MLC_MOLDLVL1LEN
                                       ,@MLC_MOLDLVL2
                                       ,@MLC_MOLDLVL2LEN
                                       ,@MLC_MOLDLVL3
                                       ,@MLC_MOLDLVL3LEN
                                       ,@MLC_MOLDLVL4
                                       ,@MLC_MOLDLVL4LEN
                                       ,@MLC_MOLDLVL5
                                       ,@MLC_MOLDLVL5LEN
                                       ,@MLC_MOLDLVL6
                                       ,@MLC_MOLDLVL6LEN
                                       ,@MLC_MOLDLVL7
                                       ,@MLC_MOLDLVL7LEN
                                       ,@MLC_MOLDLVL8
                                       ,@MLC_MOLDLVL8LEN
                                       ,@MLC_MOLDLVL9
                                       ,@MLC_MOLDLVL9LEN
                                       ,@MLC_MOLDLVLSTART
                                       ,@MLC_PINPRESET1
                                       ,@MLC_PINPRESET2
                                       ,@MLC_PINPRESET3
                                       ,@MLC_PINPRESET4
                                       ,@MLC_PINPRESET5
                                       ,@MLC_PINPRESET6
                                       ,@MLC_PINPRESET7
                                       ,@MLC_PINPRESET8
                                       ,@MLC_PINPRESET9
                                       ,@MLC_PINPRESET10
                                       ,@MLC_PINPRESET11
                                       ,@MLC_PINPRESET12
                                       ,@MLC_PINPRESET13
                                       ,@MLC_PINPRESET14
                                       ,@MLC_PINPRESET15
                                       ,@MLC_UNCNTRLDFILLDIST
                                       ,@MTL_DEGASTEMP
                                       ,@ROD_RODSPEED
                                       ,@SPD_CASTSPD1
                                       ,@SPD_CASTSPD1LEN
                                       ,@SPD_CASTSPD2
                                       ,@SPD_CASTSPD2LEN
                                       ,@SPD_CASTSPD3
                                       ,@SPD_CASTSPD3LEN
                                       ,@SPD_CASTSPD4
                                       ,@SPD_CASTSPD4LEN
                                       ,@SPD_CASTSPD5
                                       ,@SPD_CASTSPD5LEN
                                       ,@SPD_CASTSPD6
                                       ,@SPD_CASTSPD6LEN
                                       ,@SPD_CASTSPD7
                                       ,@SPD_CASTSPD7LEN
                                       ,@SPD_CASTSPD8
                                       ,@SPD_CASTSPD8LEN
                                       ,@SPD_CASTSPD9
                                       ,@SPD_CASTSPD9LEN
                                       ,@SPD_CASTSPDEND
                                       ,@SPD_CASTSPDSTART
                                       ,@SPD_PLTNHOLDTIME
                                       ,@TGH_DAMPULLSP1
                                       ,@TGH_DAMPULLSP2
                                       ,@TGH_TROUGHTEMP
                                       ,@WIP_WiperOnLen
                                       ,@WIP_WiperPosition
                                       ,@WIP_WIPPOS1
                                       ,@WIP_WIPPOS1LEN
                                       ,@WIP_WIPPOS2
                                       ,@WIP_WIPPOS2LEN
                                       ,@WIP_WIPPOS3
                                       ,@WIP_WIPPOS3LEN
                                       ,@WIP_WIPPOS4
                                       ,@WIP_WIPPOS4LEN
                                       ,@WIP_WIPPOS5
                                       ,@WIP_WIPPOS5LEN
                                       ,@WIP_WIPPOS6
                                       ,@WIP_WIPPOS6LEN
                                       ,@WIP_WIPPOS7
                                       ,@WIP_WIPPOS7LEN
                                       ,@WIP_WIPPOS8
                                       ,@WIP_WIPPOS8LEN
                                       ,@WIP_WIPPOS9
                                       ,@WIP_WIPPOS9LEN
                                       ,@WIP_WIPPOSEND
                                       ,@WIP_WIPPOSSTART
                                       ,@WTR_WATEROFFDELAY
                                       ,@WTR_WTRFLOW1
                                       ,@WTR_WTRFLOW1LEN
                                       ,@WTR_WTRFLOW2
                                       ,@WTR_WTRFLOW2LEN
                                       ,@WTR_WTRFLOW3
                                       ,@WTR_WTRFLOW3LEN
                                       ,@WTR_WTRFLOW4
                                       ,@WTR_WTRFLOW4LEN
                                       ,@WTR_WTRFLOW5
                                       ,@WTR_WTRFLOW5LEN
                                       ,@WTR_WTRFLOW6
                                       ,@WTR_WTRFLOW6LEN
                                       ,@WTR_WTRFLOW7
                                       ,@WTR_WTRFLOW7LEN
                                       ,@WTR_WTRFLOW8
                                       ,@WTR_WTRFLOW8LEN
                                       ,@WTR_WTRFLOW9
                                       ,@WTR_WTRFLOW9LEN
                                       ,@WTR_WTRFLOWEND
                                       ,@WTR_WTRFLOWSTART
                                       ,@WTR_WTRTEMPHILIMIT
                                       ,@WTR_WTRTEMPLOLIMIT
                                       ,@MSC_COOLDOWNTIME
                                       ,@MSC_MOLDDIAMETER
                                       ,@MSC_MOLDHEIGHT
                                       ,@MSC_MOLDTHICKNESS
                                       ,@MSC_MOLDWIDTH
                                       ,@MSC_RECNAME
                                       ,@MSC_NUMBERMOLDS
                                       ,@MSC_REV
                                       ,@MSC_UserDef1
                                       ,@MSC_UserDef2
                                       ,@MSC_UserDef3
                                       ,@MSC_UserDef4
                                       ,@MSC_UserDef5
                                       ,@MSC_UserDef6
                                       ,@MSC_UserDef7
                                       ,@MSC_UserDef8
                                       ,@MSC_UserDef9
                                       ,@MSC_UserDef10
                                       ,@MSC_Description
                                       ,@MSC_Alloy
                                       ,@MSC_MoldTech
                                       ,@MSC_DateCreated
                                       ,NULL
                                       ,@MSC_CreatedBy
                                       ,@MSC_Type)";
                CreateModel.MSC_DateCreated = DateTime.Now;
                CreateModel.MSC_CreatedBy = LogTool.CurrentUser.UserId;
                RecipeTable recipeTable = CreateModel;
                //如果插入数据库成功
                if (DapperToolsThisSystem.Add<RecipeTable>(sql, recipeTable) > 0)
                {
                    //执行取消命令
                    CancelCommand.Execute(obj);
                }
            }
            else
            {
                MessageBox.Show("配方已存在，请更换配方名或者版本号！", "警告", MessageBoxButton.OK, MessageBoxImage.Warning);
            }
        }
        /// <summary>
        /// 查看配方是否存在
        /// 查看条件是相同配方名和相同版本号
        /// </summary>
        /// <returns></returns>
        private bool RecipeIsNotExist()
        {
            string sql = @"SELECT * FROM RecipeTable WHERE RecipeName = @RecipeName AND MSC_REV = @MSC_REV";
            if (DapperToolsThisSystem.Query<RecipeTable>(sql, CreateModel) != null)
            {
                return false;
            }
            return true;
        }
        /// <summary>
        /// 取消命令处理方法
        /// </summary>
        /// <param name="obj"></param>
        private void CancelHandler(object obj)
        {
            //初始化本页面数据
            CreateModel = new RecipeTableByVerification();
            //重新加载数据
            Messenger.Default.Send<string>("", "ReLoadRecipeManagementPageData");
            //跳转到前一页
            Messenger.Default.Send<string>("PrevPage", "GoToPage");
        }

        /// <summary>
        /// 初始化标签
        /// </summary>
        private void InitTag()
        {
            CreateModel.FRN_FRNTILTBACKOFFSET_Tag = GetTagByItemName("FRN_FRNTILTBACKOFFSET");
            CreateModel.FRN_FRNTILTBACKOFFSET_DataTag = GetDataTagByItemName("FRN_FRNTILTBACKOFFSET");
            CreateModel.FRN_FRNBATHTEMPSP_Tag = GetTagByItemName("FRN_FRNBATHTEMPSP");
            CreateModel.FRN_FRNBATHTEMPSP_DataTag = GetDataTagByItemName("FRN_FRNBATHTEMPSP");
            CreateModel.FRN_FRNBATHTEMPHILIM_Tag = GetTagByItemName("FRN_FRNBATHTEMPHILIM");
            CreateModel.FRN_FRNBATHTEMPHILIM_DataTag = GetDataTagByItemName("FRN_FRNBATHTEMPHILIM");
            CreateModel.FRN_FRNBATHTEMPLOLIM_Tag = GetTagByItemName("FRN_FRNBATHTEMPLOLIM");
            CreateModel.FRN_FRNBATHTEMPLOLIM_DataTag = GetDataTagByItemName("FRN_FRNBATHTEMPLOLIM");
            CreateModel.TGH_TROUGHTEMP_Tag = GetTagByItemName("TGH_TROUGHTEMP");
            CreateModel.TGH_TROUGHTEMP_DataTag = GetDataTagByItemName("TGH_TROUGHTEMP");
            CreateModel.ROD_RODSPEED_Tag = GetTagByItemName("ROD_RODSPEED");
            CreateModel.ROD_RODSPEED_DataTag = GetDataTagByItemName("ROD_RODSPEED");
            CreateModel.LEN_CASTLENGTH_Tag = GetTagByItemName("LEN_CASTLENGTH");
            CreateModel.LEN_CASTLENGTH_DataTag = GetDataTagByItemName("LEN_CASTLENGTH");
            CreateModel.MTL_DEGASTEMP_Tag = GetTagByItemName("MTL_DEGASTEMP");
            CreateModel.MTL_DEGASTEMP_DataTag = GetDataTagByItemName("MTL_DEGASTEMP");
            CreateModel.GAS_IDLECASTGAS_Tag = GetTagByItemName("GAS_IDLECASTGAS");
            CreateModel.GAS_IDLECASTGAS_DataTag = GetDataTagByItemName("GAS_IDLECASTGAS");
            CreateModel.GAS_STARTCASTGASLEN_Tag = GetTagByItemName("GAS_STARTCASTGASLEN");
            CreateModel.GAS_STARTCASTGASLEN_DataTag = GetDataTagByItemName("GAS_STARTCASTGASLEN");
            CreateModel.GAS_STARTCASTGAS_Tag = GetTagByItemName("GAS_STARTCASTGAS");
            CreateModel.GAS_STARTCASTGAS_DataTag = GetDataTagByItemName("GAS_STARTCASTGAS");
            CreateModel.GAS_BEGINRAMPDOWN_Tag = GetTagByItemName("GAS_BEGINRAMPDOWN");
            CreateModel.GAS_BEGINRAMPDOWN_DataTag = GetDataTagByItemName("GAS_BEGINRAMPDOWN");
            CreateModel.GAS_ENDRAMPDOWN_Tag = GetTagByItemName("GAS_ENDRAMPDOWN");
            CreateModel.GAS_ENDRAMPDOWN_DataTag = GetDataTagByItemName("GAS_ENDRAMPDOWN");
            CreateModel.GAS_RUNCASTGAS_Tag = GetTagByItemName("GAS_RUNCASTGAS");
            CreateModel.GAS_RUNCASTGAS_DataTag = GetDataTagByItemName("GAS_RUNCASTGAS");
            CreateModel.MSC_COOLDOWNTIME_Tag = GetTagByItemName("MSC_COOLDOWNTIME");
            CreateModel.MSC_COOLDOWNTIME_DataTag = GetDataTagByItemName("MSC_COOLDOWNTIME");
            CreateModel.SPD_PLTNHOLDTIME_Tag = GetTagByItemName("SPD_PLTNHOLDTIME");
            CreateModel.SPD_PLTNHOLDTIME_DataTag = GetDataTagByItemName("SPD_PLTNHOLDTIME");
            CreateModel.WTR_WTRFLOWSTART_Tag = GetTagByItemName("WTR_WTRFLOWSTART");
            CreateModel.WTR_WTRFLOWSTART_DataTag = GetDataTagByItemName("WTR_WTRFLOWSTART");
            CreateModel.WTR_WTRFLOW1_Tag = GetTagByItemName("WTR_WTRFLOW1");
            CreateModel.WTR_WTRFLOW1_DataTag = GetDataTagByItemName("WTR_WTRFLOW1");
            CreateModel.WTR_WTRFLOW2_Tag = GetTagByItemName("WTR_WTRFLOW2");
            CreateModel.WTR_WTRFLOW2_DataTag = GetDataTagByItemName("WTR_WTRFLOW2");
            CreateModel.WTR_WTRFLOW3_Tag = GetTagByItemName("WTR_WTRFLOW3");
            CreateModel.WTR_WTRFLOW3_DataTag = GetDataTagByItemName("WTR_WTRFLOW3");
            CreateModel.WTR_WTRFLOW4_Tag = GetTagByItemName("WTR_WTRFLOW4");
            CreateModel.WTR_WTRFLOW4_DataTag = GetDataTagByItemName("WTR_WTRFLOW4");
            CreateModel.WTR_WTRFLOW5_Tag = GetTagByItemName("WTR_WTRFLOW5");
            CreateModel.WTR_WTRFLOW5_DataTag = GetDataTagByItemName("WTR_WTRFLOW5");
            CreateModel.WTR_WTRFLOW6_Tag = GetTagByItemName("WTR_WTRFLOW6");
            CreateModel.WTR_WTRFLOW6_DataTag = GetDataTagByItemName("WTR_WTRFLOW6");
            CreateModel.WTR_WTRFLOW1LEN_Tag = GetTagByItemName("WTR_WTRFLOW1LEN");
            CreateModel.WTR_WTRFLOW1LEN_DataTag = GetDataTagByItemName("WTR_WTRFLOW1LEN");
            CreateModel.WTR_WTRFLOW2LEN_Tag = GetTagByItemName("WTR_WTRFLOW2LEN");
            CreateModel.WTR_WTRFLOW2LEN_DataTag = GetDataTagByItemName("WTR_WTRFLOW2LEN");
            CreateModel.WTR_WTRFLOW3LEN_Tag = GetTagByItemName("WTR_WTRFLOW3LEN");
            CreateModel.WTR_WTRFLOW3LEN_DataTag = GetDataTagByItemName("WTR_WTRFLOW3LEN");
            CreateModel.WTR_WTRFLOW4LEN_Tag = GetTagByItemName("WTR_WTRFLOW4LEN");
            CreateModel.WTR_WTRFLOW4LEN_DataTag = GetDataTagByItemName("WTR_WTRFLOW4LEN");
            CreateModel.WTR_WTRFLOW5LEN_Tag = GetTagByItemName("WTR_WTRFLOW5LEN");
            CreateModel.WTR_WTRFLOW5LEN_DataTag = GetDataTagByItemName("WTR_WTRFLOW5LEN");
            CreateModel.WTR_WTRFLOW6LEN_Tag = GetTagByItemName("WTR_WTRFLOW6LEN");
            CreateModel.WTR_WTRFLOW6LEN_DataTag = GetDataTagByItemName("WTR_WTRFLOW6LEN");
            CreateModel.WTR_WTRFLOWEND_Tag = GetTagByItemName("WTR_WTRFLOWEND");
            CreateModel.WTR_WTRFLOWEND_DataTag = GetDataTagByItemName("WTR_WTRFLOWEND");
            CreateModel.WTR_WTRTEMPHILIMIT_Tag = GetTagByItemName("WTR_WTRTEMPHILIMIT");
            CreateModel.WTR_WTRTEMPHILIMIT_DataTag = GetDataTagByItemName("WTR_WTRTEMPHILIMIT");
            CreateModel.WTR_WTRTEMPLOLIMIT_Tag = GetTagByItemName("WTR_WTRTEMPLOLIMIT");
            CreateModel.WTR_WTRTEMPLOLIMIT_DataTag = GetDataTagByItemName("WTR_WTRTEMPLOLIMIT");
            CreateModel.SPD_CASTSPD1LEN_Tag = GetTagByItemName("SPD_CASTSPD1LEN");
            CreateModel.SPD_CASTSPD1LEN_DataTag = GetDataTagByItemName("SPD_CASTSPD1LEN");
            CreateModel.SPD_CASTSPD2LEN_Tag = GetTagByItemName("SPD_CASTSPD2LEN");
            CreateModel.SPD_CASTSPD2LEN_DataTag = GetDataTagByItemName("SPD_CASTSPD2LEN");
            CreateModel.SPD_CASTSPD3LEN_Tag = GetTagByItemName("SPD_CASTSPD3LEN");
            CreateModel.SPD_CASTSPD3LEN_DataTag = GetDataTagByItemName("SPD_CASTSPD3LEN");
            CreateModel.SPD_CASTSPD4LEN_Tag = GetTagByItemName("SPD_CASTSPD4LEN");
            CreateModel.SPD_CASTSPD4LEN_DataTag = GetDataTagByItemName("SPD_CASTSPD4LEN");
            CreateModel.SPD_CASTSPD5LEN_Tag = GetTagByItemName("SPD_CASTSPD5LEN");
            CreateModel.SPD_CASTSPD5LEN_DataTag = GetDataTagByItemName("SPD_CASTSPD5LEN");
            CreateModel.SPD_CASTSPD6LEN_Tag = GetTagByItemName("SPD_CASTSPD6LEN");
            CreateModel.SPD_CASTSPD6LEN_DataTag = GetDataTagByItemName("SPD_CASTSPD6LEN");
            CreateModel.SPD_CASTSPDSTART_Tag = GetTagByItemName("SPD_CASTSPDSTART");
            CreateModel.SPD_CASTSPDSTART_DataTag = GetDataTagByItemName("SPD_CASTSPDSTART");
            CreateModel.SPD_CASTSPD1_Tag = GetTagByItemName("SPD_CASTSPD1");
            CreateModel.SPD_CASTSPD1_DataTag = GetDataTagByItemName("SPD_CASTSPD1");
            CreateModel.SPD_CASTSPD2_Tag = GetTagByItemName("SPD_CASTSPD2");
            CreateModel.SPD_CASTSPD2_DataTag = GetDataTagByItemName("SPD_CASTSPD2");
            CreateModel.SPD_CASTSPD3_Tag = GetTagByItemName("SPD_CASTSPD3");
            CreateModel.SPD_CASTSPD3_DataTag = GetDataTagByItemName("SPD_CASTSPD3");
            CreateModel.SPD_CASTSPD4_Tag = GetTagByItemName("SPD_CASTSPD4");
            CreateModel.SPD_CASTSPD4_DataTag = GetDataTagByItemName("SPD_CASTSPD4");
            CreateModel.SPD_CASTSPD5_Tag = GetTagByItemName("SPD_CASTSPD5");
            CreateModel.SPD_CASTSPD5_DataTag = GetDataTagByItemName("SPD_CASTSPD5");
            CreateModel.SPD_CASTSPD6_Tag = GetTagByItemName("SPD_CASTSPD6");
            CreateModel.SPD_CASTSPD6_DataTag = GetDataTagByItemName("SPD_CASTSPD6");
            CreateModel.SPD_CASTSPDEND_Tag = GetTagByItemName("SPD_CASTSPDEND");
            CreateModel.SPD_CASTSPDEND_DataTag = GetDataTagByItemName("SPD_CASTSPDEND");
            CreateModel.LUB_PULSELUBEPULSES_Tag = GetTagByItemName("LUB_PULSELUBEPULSES");
            CreateModel.LUB_PULSELUBEPULSES_DataTag = GetDataTagByItemName("LUB_PULSELUBEPULSES");
            CreateModel.LUB_PULSELUBEONTIME_Tag = GetTagByItemName("LUB_PULSELUBEONTIME");
            CreateModel.LUB_PULSELUBEONTIME_DataTag = GetDataTagByItemName("LUB_PULSELUBEONTIME");
            CreateModel.LUB_PULSELUBEOFFTIME_Tag = GetTagByItemName("LUB_PULSELUBEOFFTIME");
            CreateModel.LUB_PULSELUBEOFFTIME_DataTag = GetDataTagByItemName("LUB_PULSELUBEOFFTIME");
            CreateModel.LUB_PULSELUBECYCTIME_Tag = GetTagByItemName("LUB_PULSELUBECYCTIME");
            CreateModel.LUB_PULSELUBECYCTIME_DataTag = GetDataTagByItemName("LUB_PULSELUBECYCTIME");
            CreateModel.LUB_PULSELUBEPRESFIRE_Tag = GetTagByItemName("LUB_PULSELUBEPRESFIRE");
            CreateModel.LUB_PULSELUBEPRESFIRE_DataTag = GetDataTagByItemName("LUB_PULSELUBEPRESFIRE");
            CreateModel.LUB_PULSELUBEPRESRESET_Tag = GetTagByItemName("LUB_PULSELUBEPRESRESET");
            CreateModel.LUB_PULSELUBEPRESRESET_DataTag = GetDataTagByItemName("LUB_PULSELUBEPRESRESET");
            CreateModel.LUB_PULSELUBEPRESHOLD_Tag = GetTagByItemName("LUB_PULSELUBEPRESHOLD");
            CreateModel.LUB_PULSELUBEPRESHOLD_DataTag = GetDataTagByItemName("LUB_PULSELUBEPRESHOLD");
        }

        private Meta_Tag GetTagByItemName(string ItemName)
        {
            Meta_Tag meta_Tag = new Meta_Tag();
            string sql = @"SELECT * 
                          FROM DB_AlarmServer.dbo.Meta_Tag 
                          WHERE TagID = (
                                    SELECT TagId 
                                    FROM DB_CWEGUI.dbo.RecipeVerificationConfig
                                    WHERE ItemName = '" + ItemName + @"'
                          )";
            return DapperToolsThisSystem.Query(sql, meta_Tag);
        }
        private Meta_Tag GetDataTagByItemName(string ItemName)
        {
            Meta_Tag meta_Tag = new Meta_Tag();
            string sql = @"SELECT * 
                          FROM DB_AlarmServer.dbo.Meta_Tag 
                          WHERE TagID = (
                                    SELECT DataTagId 
                                    FROM DB_CWEGUI.dbo.RecipeVerificationConfig
                                    WHERE ItemName = '" + ItemName + @"'
                          )";
            return DapperToolsThisSystem.Query(sql, meta_Tag);
        }

        /// <summary>
        /// 初始化默认边框颜色
        /// </summary>
        private void InitVerificationBorderBrush()
        {
            CreateModel.RecipeNameBorderBrush = ConvertToBrush("#ABADB3");
            CreateModel.MSC_MOLDDIAMETERBorderBrush = ConvertToBrush("#ABADB3");
            CreateModel.MSC_REVBorderBrush = ConvertToBrush("#ABADB3");
            CreateModel.MSC_MoldTechBorderBrush = ConvertToBrush("#ABADB3");
            CreateModel.MSC_NUMBERMOLDSBorderBrush = ConvertToBrush("#ABADB3");
            CreateModel.MSC_DescriptionBorderBrush = ConvertToBrush("#ABADB3");
            CreateModel.FRN_FRNTILTBACKOFFSETBorderBrush = ConvertToBrush("#ABADB3");
            CreateModel.FRN_FRNBATHTEMPSPBorderBrush = ConvertToBrush("#ABADB3");
            CreateModel.FRN_FRNBATHTEMPHILIMBorderBrush = ConvertToBrush("#ABADB3");
            CreateModel.FRN_FRNBATHTEMPLOLIMBorderBrush = ConvertToBrush("#ABADB3");
            CreateModel.TGH_TROUGHTEMPBorderBrush = ConvertToBrush("#ABADB3");
            CreateModel.ROD_RODSPEEDBorderBrush = ConvertToBrush("#ABADB3");
            CreateModel.LEN_CASTLENGTHBorderBrush = ConvertToBrush("#ABADB3");
            CreateModel.MTL_DEGASTEMPBorderBrush = ConvertToBrush("#ABADB3");
            CreateModel.GAS_IDLECASTGASBorderBrush = ConvertToBrush("#ABADB3");
            CreateModel.GAS_STARTCASTGASLENBorderBrush = ConvertToBrush("#ABADB3");
            CreateModel.GAS_STARTCASTGASBorderBrush = ConvertToBrush("#ABADB3");
            CreateModel.GAS_BEGINRAMPDOWNBorderBrush = ConvertToBrush("#ABADB3");
            CreateModel.GAS_ENDRAMPDOWNBorderBrush = ConvertToBrush("#ABADB3");
            CreateModel.GAS_RUNCASTGASBorderBrush = ConvertToBrush("#ABADB3");
            CreateModel.MSC_COOLDOWNTIMEBorderBrush = ConvertToBrush("#ABADB3");
            CreateModel.SPD_PLTNHOLDTIMEBorderBrush = ConvertToBrush("#ABADB3");
            CreateModel.WTR_WTRFLOW1LENBorderBrush = ConvertToBrush("#ABADB3");
            CreateModel.WTR_WTRFLOW2LENBorderBrush = ConvertToBrush("#ABADB3");
            CreateModel.WTR_WTRFLOW3LENBorderBrush = ConvertToBrush("#ABADB3");
            CreateModel.WTR_WTRFLOW4LENBorderBrush = ConvertToBrush("#ABADB3");
            CreateModel.WTR_WTRFLOW5LENBorderBrush = ConvertToBrush("#ABADB3");
            CreateModel.WTR_WTRFLOW6LENBorderBrush = ConvertToBrush("#ABADB3");
            CreateModel.WTR_WTRFLOWSTARTBorderBrush = ConvertToBrush("#ABADB3");
            CreateModel.WTR_WTRFLOW1BorderBrush = ConvertToBrush("#ABADB3");
            CreateModel.WTR_WTRFLOW2BorderBrush = ConvertToBrush("#ABADB3");
            CreateModel.WTR_WTRFLOW3BorderBrush = ConvertToBrush("#ABADB3");
            CreateModel.WTR_WTRFLOW4BorderBrush = ConvertToBrush("#ABADB3");
            CreateModel.WTR_WTRFLOW5BorderBrush = ConvertToBrush("#ABADB3");
            CreateModel.WTR_WTRFLOW6BorderBrush = ConvertToBrush("#ABADB3");
            CreateModel.WTR_WTRFLOWENDBorderBrush = ConvertToBrush("#ABADB3");
            CreateModel.WTR_WTRTEMPHILIMITBorderBrush = ConvertToBrush("#ABADB3");
            CreateModel.WTR_WTRTEMPLOLIMITBorderBrush = ConvertToBrush("#ABADB3");
            CreateModel.SPD_CASTSPD1LENBorderBrush = ConvertToBrush("#ABADB3");
            CreateModel.SPD_CASTSPD2LENBorderBrush = ConvertToBrush("#ABADB3");
            CreateModel.SPD_CASTSPD3LENBorderBrush = ConvertToBrush("#ABADB3");
            CreateModel.SPD_CASTSPD4LENBorderBrush = ConvertToBrush("#ABADB3");
            CreateModel.SPD_CASTSPD5LENBorderBrush = ConvertToBrush("#ABADB3");
            CreateModel.SPD_CASTSPD6LENBorderBrush = ConvertToBrush("#ABADB3");
            CreateModel.SPD_CASTSPDSTARTBorderBrush = ConvertToBrush("#ABADB3");
            CreateModel.SPD_CASTSPD1BorderBrush = ConvertToBrush("#ABADB3");
            CreateModel.SPD_CASTSPD2BorderBrush = ConvertToBrush("#ABADB3");
            CreateModel.SPD_CASTSPD3BorderBrush = ConvertToBrush("#ABADB3");
            CreateModel.SPD_CASTSPD4BorderBrush = ConvertToBrush("#ABADB3");
            CreateModel.SPD_CASTSPD5BorderBrush = ConvertToBrush("#ABADB3");
            CreateModel.SPD_CASTSPD6BorderBrush = ConvertToBrush("#ABADB3");
            CreateModel.SPD_CASTSPDENDBorderBrush = ConvertToBrush("#ABADB3");
            CreateModel.LUB_PULSELUBEPULSESBorderBrush = ConvertToBrush("#ABADB3");
            CreateModel.LUB_PULSELUBEONTIMEBorderBrush = ConvertToBrush("#ABADB3");
            CreateModel.LUB_PULSELUBEOFFTIMEBorderBrush = ConvertToBrush("#ABADB3");
            CreateModel.LUB_PULSELUBECYCTIMEBorderBrush = ConvertToBrush("#ABADB3");
            CreateModel.LUB_PULSELUBEPRESFIREBorderBrush = ConvertToBrush("#ABADB3");
            CreateModel.LUB_PULSELUBEPRESRESETBorderBrush = ConvertToBrush("#ABADB3");
            CreateModel.LUB_PULSELUBEPRESHOLDBorderBrush = ConvertToBrush("#ABADB3");
            CreateModel.MSC_AlloyBorderBrush = ConvertToBrush("#ABADB3");
        }
        /// <summary>
        /// 颜色转换
        /// </summary>
        /// <param name="htmlcolor"></param>
        /// <returns></returns>
        private SolidColorBrush ConvertToBrush(string htmlcolor)
        {
            if (htmlcolor != null)
            {
                Color color = (Color)System.Windows.Media.ColorConverter.ConvertFromString(htmlcolor);
                return new SolidColorBrush(color);
            }
            return null;
        }
    }
}
